/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.devkit;

import org.apache.tools.ant.BuildException;

/**
 *
 */
public class Main
{
	public static void main(String[] args)
	{
		GeneratorTask task = new GeneratorTask();

		boolean showUsage = false;

		if(args == null || args.length < 1)
		{
			showUsage = true;
		}

		for(int i = 0; args != null && i < args.length; i++)
		{
			String a = args[i];
			String an = i < args.length - 1 ? args[i + 1] : "";

			// -h|-?|help: show usage
			if(a.startsWith("-h") || a.equals("-?") || a.equalsIgnoreCase("help"))
			{
				showUsage = true;
				break;
			}
			else if(a.startsWith("-i"))
			{
				// TODO interactive
			}
			// -s: should generate service delegates
			else if(a.startsWith("-s")) task.setGenerateservicecode(String.valueOf(true));

			// -c: should generate client code
			else if(a.startsWith("-c")) task.setGenerateclientcode(String.valueOf(true));

			// -p: should generate project layout
			else if(a.startsWith("-p")) task.setGenerateprojectlayout(String.valueOf(true));

			// -v: should use verbose mode for code generation
			else if(a.startsWith("-v")) task.setVerbose(String.valueOf(true));

			// -o || -d: location of the generated output
			else if(a.startsWith("-o") || a.startsWith("-d")) task.setOutputlocation(an);

			// -u || -f: URL or path location of the endpoint descriptor
			else if(a.startsWith("-u") || a.startsWith("-f")) task.setDescriptorurl(an);

			// -t <type>: type of layout that should be created
			else if(a.startsWith("-t")) task.setCodegentype(an);
		}

		if(showUsage) usage();
		else
		{
			try
			{
				task.execute();
			}
			catch(BuildException be)
			{
				if(be instanceof ValidationException)
				{
					System.err.println(be.getMessage());
					usage();
				}
				else
				{
					be.printStackTrace();
				}
			}
		}
	}

	private static void usage()
	{
		System.out.println("Usage:");
		System.out.println("------");
		System.out.println("");
		System.out.println("$java " + Main.class.getName() + " <options>");
		System.out.println("");
		System.out.println("Where, <options> are:");
		System.out.println("  -u|-f <location-of-descriptor>: ");
		System.out.println("\t" + "URL or path of the code-gen configuration file if accompanied by -s flag.");
		System.out.println("\t" + "URL or path of the endpoint descriptor if accompanied by -c flag.");
		System.out.println("  [-s]: ");
		System.out.println("\t" + "Indicate that service delegate code should be generated. Default = false");
		System.out.println("  [-c]: ");
		System.out.println("\t" + "Indicate that client code should be generated. Default = false");
		System.out.println("  [-p]: ");
		System.out.println("\t" + "Flag to indicate that an Eclipse project layout should be created. Default = false");
		System.out.println("  [-o|-d <output-directory-of-generated-code>]: ");
		System.out.println("\t" + "Path where the code will be generated. Default = './generated-code'");
		System.out.println("  [-t cmdline|war|ear]: ");
		System.out.println("\t" + "Type of the generated code; comand line program, as a WAR or as an EAR. Default = cmdline");
		System.out.println("  [-v]: ");
		System.out.println("\t" + "Indicate high verbosity console output when generating code.");
		System.out.println("  [-h|help|-?]: ");
		System.out.println("\t" + "Show usage.");
		System.out.println("");
		System.out.println("Examples:");
		System.out.println("---------");
		System.out.println("$java " + Main.class.getName() + " -f C:/temp/cgconfig.xml -d C:/temp/myCodeGenDir -s");
		System.out.println("");
		System.out.println("Generates the service delegate code using the given code generation configuration file in the given output directory");
		System.out.println("--");
		System.out.println("");
		System.out.println("$java " + Main.class.getName() + " -u http://myhost/sampleService/layout/xml -c -v -p");
		System.out.println("");
		System.out.println("Generates the client code using the given endpoint descriptor URL in the default output directory; also creates the Eclipse project layout");
		System.out.println("--");
		System.out.println("");
	}
}
